local assets = {
	Asset("ANIM", "anim/musha_ironcathat.zip"),
	Asset("ANIM", "anim/musha_ironcathat_goggles.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_ironcathat.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_ironcathat.tex")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end
	inst.components.insulator.insulation =
		inst.components.musha_equipment.currentlevel > 10 and TUNING.INSULATION_SMALL or TUNING.INSULATION_MED
	inst.components.armor:SetAbsorption(
		math.max(45 + inst.components.musha_equipment.currentlevel, 30 + inst.components.musha_equipment.currentlevel * 2) *
			0.01
	)
	if display then
		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_HAT_CAT .. " (LV%d)\n" .. STRINGS.MUSHA_ARMOR .. " (%d)\n" .. STRINGS.MUSHA_ITEM_WARMNCOOL .. "(%s)",
				inst.components.musha_equipment.currentlevel,
				math.max(45 + inst.components.musha_equipment.currentlevel, 30 + inst.components.musha_equipment.currentlevel * 2),
				inst.components.musha_equipment.currentlevel > 10 and "SMALL" or "MED"
			)
		)
	end
end

local function ChangeInsulation(inst)
	if TheWorld.state.issummer then
		inst.components.insulator:SetSummer()
	elseif not TheWorld.state.issummer then
		inst.components.insulator:SetWinter()
	end
end

local function OnEquip(inst, owner)
	ChangeInsulation(inst)
	inst:WatchWorldState("season", ChangeInsulation)

	inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
	owner.AnimState:OverrideSymbol("swap_hat", "musha_ironcathat", "swap_hat")
	owner.AnimState:Hide("HAIR")
	owner.AnimState:Hide("HAIR_NOHAT")
	owner.AnimState:Show("HAT_HAIR")
	owner.AnimState:Show("HAT")
	Updata(inst)
end

local function Energy_Switch(inst, data, owner, fname_override)
	local owner = inst.components.inventoryitem.owner
	if not inst.EnergyOn then
		if inst.components.musha_equipment:IsBroken() then
			return
		end
		inst.components.musha_equipment:StartConsuming()
		SpawnPrefab("collapse_small").Transform:SetPosition(inst:GetPosition():Get())
		inst.SoundEmitter:PlaySound("dontstarve/common/minerhatAddFuel")
		owner.AnimState:OverrideSymbol("swap_hat", "musha_ironcathat_goggles", "swap_hat")
		inst.Light:Enable(true)
	else
		inst.components.musha_equipment:StartConsuming()
		inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
		owner.AnimState:OverrideSymbol("swap_hat", "musha_ironcathat", "swap_hat")
		inst.Light:Enable(false)
	end
	inst.EnergyOn = not inst.EnergyOn
	Updata(inst)
	inst.components.useableitem:StopUsingItem()
end

local function OnUnequip(inst, owner)
	if inst.EnergyOn then
		Energy_Switch(inst)
	end
	inst.Light:Enable(false)

	owner.AnimState:Hide("HAT")
	owner.AnimState:Hide("HAT_HAIR")
	owner.AnimState:Show("HAIR_NOHAT")
	owner.AnimState:Show("HAIR")
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst.entity:AddLight()
	inst.Light:SetFalloff(0.4)
	inst.Light:SetIntensity(.7)
	inst.Light:SetRadius(2.5)
	inst.Light:SetColour(155 / 255, 175 / 255, 195 / 255)
	inst.Light:Enable(false)

	inst.AnimState:SetBank("hat_mwildcat")
	inst.AnimState:SetBuild("musha_ironcathat")

	inst.AnimState:PlayAnimation("idle")

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		return inst
	end

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(Energy_Switch)

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("hat", 850, 0.09)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.EnergyOn then
				Energy_Switch(inst)
				inst.EnergyOn = true
			end
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.equippable.walkspeedmult = 1
				inst.components.talker:Say(
					STRINGS.MUSHA_HAT_BROKEN .. "\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)\n" .. STRINGS.MUSHA_ITEM_SPEED .. "(0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_ironcathat.xml"

	inst:AddComponent("insulator")
	inst.EnergyOn = false
	inst.type = "fire"

	--[[ inst.Rebind = function(inst)
		if inst.EnergyOn then
			inst.EnergyOn = false
			Energy_Switch(inst)
		end
	end ]]
	return inst
end

return Prefab("musha_ironcathat", fn, assets)
